package ar.com.globallogic.promomr.persistance;

import java.util.List;

import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import ar.com.globallogic.promomr.domain.Publication;

@Repository
public interface PublicationRepository extends CrudRepository<Publication, String>{
	
	@Query("{'content': {'$ref': 'contents', '$id': {'$oid': ?0 }}}")
	public Publication findByContentId(String contentId);
	
	@Query(value="{'state._class':'ar.com.globallogic.promomr.domain.PendingState'}")
	public List<Publication> findAllPendingPublications();
	
	@Query(value="{$and: [" +
					"{'content': {'$ref': 'contents', '$id': {'$oid': ?0 }}}, "+
					"{ $or: [" +
						"{'state._class':'ar.com.globallogic.promomr.domain.PublishedState'}, " +
						"{'owner': {'$ref': 'users', '$id': {'$oid': ?1 }}} "+
					"]}" +
				"]}")
	public Publication findByContentIdForUser(String contentId, String userId);
}
